package ledger

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 余额查询 ====================

// BalanceQueryRequest 余额查询请求
type BalanceQueryRequest struct {
	models.OrgBaseRequest
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// AccountType 账户类型
	AccountType string `json:"account_type,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewBalanceQueryRequest 创建余额查询请求
func NewBalanceQueryRequest() *BalanceQueryRequest {
	req := &BalanceQueryRequest{}
	return req
}

// SetOrgCode 设置机构代码
func (r *BalanceQueryRequest) SetOrgCode(orgCode string) *BalanceQueryRequest {
	r.OrgCode = orgCode
	return r
}

// SetMchID 设置商户号
func (r *BalanceQueryRequest) SetMchID(mchID string) *BalanceQueryRequest {
	r.MchID = mchID
	return r
}

// SetAccountType 设置账户类型
func (r *BalanceQueryRequest) SetAccountType(accountType string) *BalanceQueryRequest {
	r.AccountType = accountType
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *BalanceQueryRequest) SetRawRequestMap(rawMap map[string]interface{}) *BalanceQueryRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *BalanceQueryRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *BalanceQueryRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.OrgCode != "" {
		baseMap["org_code"] = r.OrgCode
	}
	if r.MchID != "" {
		baseMap["mch_id"] = r.MchID
	}
	if r.AccountType != "" {
		baseMap["account_type"] = r.AccountType
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *BalanceQueryRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// BalanceQueryResponse 余额查询响应
type BalanceQueryResponse struct {
	models.StandardResponse
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// AccountType 账户类型
	AccountType string `json:"account_type,omitempty"`
	// Balance 余额
	Balance interface{} `json:"balance,omitempty"`
	// AvailableBalance 可用余额
	AvailableBalance interface{} `json:"available_balance,omitempty"`
}

// NewBalanceQueryResponse 创建余额查询响应
func NewBalanceQueryResponse() *BalanceQueryResponse {
	return &BalanceQueryResponse{}
}

// ==================== 余额分账 ====================

// BalanceSplitRequest 余额分账请求
type BalanceSplitRequest struct {
	models.OrgBaseRequest
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// TotalAmount 分账总金额
	TotalAmount string `json:"total_amount,omitempty"`
	// Receivers 分账接收方列表
	Receivers []*Receiver `json:"receivers,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewBalanceSplitRequest 创建余额分账请求
func NewBalanceSplitRequest() *BalanceSplitRequest {
	req := &BalanceSplitRequest{}
	return req
}

// SetOrgCode 设置机构代码
func (r *BalanceSplitRequest) SetOrgCode(orgCode string) *BalanceSplitRequest {
	r.OrgCode = orgCode
	return r
}

// SetMchID 设置商户号
func (r *BalanceSplitRequest) SetMchID(mchID string) *BalanceSplitRequest {
	r.MchID = mchID
	return r
}

// SetOutOrderNo 设置外部订单号
func (r *BalanceSplitRequest) SetOutOrderNo(outOrderNo string) *BalanceSplitRequest {
	r.OutOrderNo = outOrderNo
	return r
}

// SetTotalAmount 设置分账总金额
func (r *BalanceSplitRequest) SetTotalAmount(totalAmount string) *BalanceSplitRequest {
	r.TotalAmount = totalAmount
	return r
}

// SetReceivers 设置分账接收方列表
func (r *BalanceSplitRequest) SetReceivers(receivers []*Receiver) *BalanceSplitRequest {
	r.Receivers = receivers
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *BalanceSplitRequest) SetRawRequestMap(rawMap map[string]interface{}) *BalanceSplitRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *BalanceSplitRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *BalanceSplitRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.OrgCode != "" {
		baseMap["org_code"] = r.OrgCode
	}
	if r.MchID != "" {
		baseMap["mch_id"] = r.MchID
	}
	if r.OutOrderNo != "" {
		baseMap["out_order_no"] = r.OutOrderNo
	}
	if r.TotalAmount != "" {
		baseMap["total_amount"] = r.TotalAmount
	}
	if r.Receivers != nil && len(r.Receivers) > 0 {
		receiversList := make([]map[string]interface{}, len(r.Receivers))
		for i, receiver := range r.Receivers {
			receiverMap, _ := receiver.ToMap()
			receiversList[i] = receiverMap
		}
		baseMap["receivers"] = receiversList
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *BalanceSplitRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// BalanceSplitResponse 余额分账响应
type BalanceSplitResponse struct {
	models.StandardResponse
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OrderNo 嘉联分账单号
	OrderNo string `json:"order_no,omitempty"`
	// TotalAmount 分账总金额
	TotalAmount interface{} `json:"total_amount,omitempty"`
	// State 状态
	State string `json:"state,omitempty"`
	// Receivers 分账接收方列表
	Receivers []*Receiver `json:"receivers,omitempty"`
}

// NewBalanceSplitResponse 创建余额分账响应
func NewBalanceSplitResponse() *BalanceSplitResponse {
	return &BalanceSplitResponse{}
}

// Receiver 接收方信息
type Receiver struct {
	// RevMchID 接收方商户号
	RevMchID string `json:"rev_mch_id,omitempty"`
	// Amount 金额
	Amount interface{} `json:"amount,omitempty"`
	// Description 描述
	Description string `json:"description,omitempty"`
	// Result 结果
	Result string `json:"result,omitempty"`
	// FailReason 失败原因
	FailReason string `json:"fail_reason,omitempty"`
	// DetailID 明细ID
	DetailID string `json:"detail_id,omitempty"`
	// CreateTime 创建时间
	CreateTime string `json:"create_time,omitempty"`
	// FinishTime 完成时间
	FinishTime string `json:"finish_time,omitempty"`
}

// NewReceiver 创建接收方信息
func NewReceiver() *Receiver {
	return &Receiver{}
}

// SetRevMchID 设置接收方商户号
func (r *Receiver) SetRevMchID(revMchID string) *Receiver {
	r.RevMchID = revMchID
	return r
}

// SetAmount 设置金额
func (r *Receiver) SetAmount(amount interface{}) *Receiver {
	r.Amount = amount
	return r
}

// SetDescription 设置描述
func (r *Receiver) SetDescription(description string) *Receiver {
	r.Description = description
	return r
}

// ToMap 转换为map
func (r *Receiver) ToMap() (map[string]interface{}, error) {
	result := make(map[string]interface{})

	if r.RevMchID != "" {
		result["rev_mch_id"] = r.RevMchID
	}
	if r.Amount != nil {
		result["amount"] = r.Amount
	}
	if r.Description != "" {
		result["description"] = r.Description
	}
	if r.Result != "" {
		result["result"] = r.Result
	}
	if r.FailReason != "" {
		result["fail_reason"] = r.FailReason
	}
	if r.DetailID != "" {
		result["detail_id"] = r.DetailID
	}
	if r.CreateTime != "" {
		result["create_time"] = r.CreateTime
	}
	if r.FinishTime != "" {
		result["finish_time"] = r.FinishTime
	}

	return result, nil
}

// ==================== 余额分账查询 ====================

// BalanceSplitQueryRequest 余额分账查询请求
type BalanceSplitQueryRequest struct {
	models.OrgBaseRequest
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OrderNo 嘉联分账单号
	OrderNo string `json:"order_no,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewBalanceSplitQueryRequest 创建余额分账查询请求
func NewBalanceSplitQueryRequest() *BalanceSplitQueryRequest {
	req := &BalanceSplitQueryRequest{}
	return req
}

// SetOrgCode 设置机构代码
func (r *BalanceSplitQueryRequest) SetOrgCode(orgCode string) *BalanceSplitQueryRequest {
	r.OrgCode = orgCode
	return r
}

// SetOutOrderNo 设置外部订单号
func (r *BalanceSplitQueryRequest) SetOutOrderNo(outOrderNo string) *BalanceSplitQueryRequest {
	r.OutOrderNo = outOrderNo
	return r
}

// SetOrderNo 设置嘉联分账单号
func (r *BalanceSplitQueryRequest) SetOrderNo(orderNo string) *BalanceSplitQueryRequest {
	r.OrderNo = orderNo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *BalanceSplitQueryRequest) SetRawRequestMap(rawMap map[string]interface{}) *BalanceSplitQueryRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *BalanceSplitQueryRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *BalanceSplitQueryRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.OrgCode != "" {
		baseMap["org_code"] = r.OrgCode
	}
	if r.OutOrderNo != "" {
		baseMap["out_order_no"] = r.OutOrderNo
	}
	if r.OrderNo != "" {
		baseMap["order_no"] = r.OrderNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *BalanceSplitQueryRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// BalanceSplitQueryResponse 余额分账查询响应
type BalanceSplitQueryResponse struct {
	models.StandardResponse
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OrderNo 嘉联分账单号
	OrderNo string `json:"order_no,omitempty"`
	// TotalAmount 分账总金额
	TotalAmount interface{} `json:"total_amount,omitempty"`
	// State 状态
	State string `json:"state,omitempty"`
	// Receivers 分账接收方列表
	Receivers []*Receiver `json:"receivers,omitempty"`
}

// NewBalanceSplitQueryResponse 创建余额分账查询响应
func NewBalanceSplitQueryResponse() *BalanceSplitQueryResponse {
	return &BalanceSplitQueryResponse{}
}

// ==================== 余额退款 ====================

// BalanceRefundRequest 余额退款请求
type BalanceRefundRequest struct {
	models.OrgBaseRequest
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// MchID 商户号
	MchID string `json:"mch_id,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OutReturnNo 外部退款号
	OutReturnNo string `json:"out_return_no,omitempty"`
	// RevMchID 接收方商户号
	RevMchID string `json:"rev_mch_id,omitempty"`
	// Amount 退款金额
	Amount string `json:"amount,omitempty"`
	// Description 退款描述
	Description string `json:"description,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewBalanceRefundRequest 创建余额退款请求
func NewBalanceRefundRequest() *BalanceRefundRequest {
	req := &BalanceRefundRequest{}
	return req
}

// SetOrgCode 设置机构代码
func (r *BalanceRefundRequest) SetOrgCode(orgCode string) *BalanceRefundRequest {
	r.OrgCode = orgCode
	return r
}

// SetMchID 设置商户号
func (r *BalanceRefundRequest) SetMchID(mchID string) *BalanceRefundRequest {
	r.MchID = mchID
	return r
}

// SetOutOrderNo 设置外部订单号
func (r *BalanceRefundRequest) SetOutOrderNo(outOrderNo string) *BalanceRefundRequest {
	r.OutOrderNo = outOrderNo
	return r
}

// SetOutReturnNo 设置外部退款号
func (r *BalanceRefundRequest) SetOutReturnNo(outReturnNo string) *BalanceRefundRequest {
	r.OutReturnNo = outReturnNo
	return r
}

// SetRevMchID 设置接收方商户号
func (r *BalanceRefundRequest) SetRevMchID(revMchID string) *BalanceRefundRequest {
	r.RevMchID = revMchID
	return r
}

// SetAmount 设置退款金额
func (r *BalanceRefundRequest) SetAmount(amount string) *BalanceRefundRequest {
	r.Amount = amount
	return r
}

// SetDescription 设置退款描述
func (r *BalanceRefundRequest) SetDescription(description string) *BalanceRefundRequest {
	r.Description = description
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *BalanceRefundRequest) SetRawRequestMap(rawMap map[string]interface{}) *BalanceRefundRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *BalanceRefundRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *BalanceRefundRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.OrgCode != "" {
		baseMap["org_code"] = r.OrgCode
	}
	if r.MchID != "" {
		baseMap["mch_id"] = r.MchID
	}
	if r.OutOrderNo != "" {
		baseMap["out_order_no"] = r.OutOrderNo
	}
	if r.OutReturnNo != "" {
		baseMap["out_return_no"] = r.OutReturnNo
	}
	if r.RevMchID != "" {
		baseMap["rev_mch_id"] = r.RevMchID
	}
	if r.Amount != "" {
		baseMap["amount"] = r.Amount
	}
	if r.Description != "" {
		baseMap["description"] = r.Description
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *BalanceRefundRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// BalanceRefundResponse 余额退款响应
type BalanceRefundResponse struct {
	models.StandardResponse
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OrderNo 嘉联分账单号
	OrderNo string `json:"order_no,omitempty"`
	// OutReturnNo 外部退款号
	OutReturnNo string `json:"out_return_no,omitempty"`
	// ReturnNo 嘉联退款号
	ReturnNo string `json:"return_no,omitempty"`
	// Amount 退款金额
	Amount interface{} `json:"amount,omitempty"`
	// Description 退款描述
	Description string `json:"description,omitempty"`
	// Result 结果
	Result string `json:"result,omitempty"`
	// FailReason 失败原因
	FailReason string `json:"fail_reason,omitempty"`
	// CreateTime 创建时间
	CreateTime string `json:"create_time,omitempty"`
	// FinishTime 完成时间
	FinishTime string `json:"finish_time,omitempty"`
}

// NewBalanceRefundResponse 创建余额退款响应
func NewBalanceRefundResponse() *BalanceRefundResponse {
	return &BalanceRefundResponse{}
}

// ==================== 余额退款查询 ====================

// BalanceRefundQueryRequest 余额退款查询请求
type BalanceRefundQueryRequest struct {
	models.OrgBaseRequest
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutReturnNo 外部退款号
	OutReturnNo string `json:"out_return_no,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewBalanceRefundQueryRequest 创建余额退款查询请求
func NewBalanceRefundQueryRequest() *BalanceRefundQueryRequest {
	req := &BalanceRefundQueryRequest{}
	return req
}

// SetOrgCode 设置机构代码
func (r *BalanceRefundQueryRequest) SetOrgCode(orgCode string) *BalanceRefundQueryRequest {
	r.OrgCode = orgCode
	return r
}

// SetOutReturnNo 设置外部退款号
func (r *BalanceRefundQueryRequest) SetOutReturnNo(outReturnNo string) *BalanceRefundQueryRequest {
	r.OutReturnNo = outReturnNo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *BalanceRefundQueryRequest) SetRawRequestMap(rawMap map[string]interface{}) *BalanceRefundQueryRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *BalanceRefundQueryRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *BalanceRefundQueryRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.OrgCode != "" {
		baseMap["org_code"] = r.OrgCode
	}
	if r.OutReturnNo != "" {
		baseMap["out_return_no"] = r.OutReturnNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *BalanceRefundQueryRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// BalanceRefundQueryResponse 余额退款查询响应
type BalanceRefundQueryResponse struct {
	models.StandardResponse
	// OrgCode 机构代码
	OrgCode string `json:"org_code,omitempty"`
	// OutOrderNo 外部订单号
	OutOrderNo string `json:"out_order_no,omitempty"`
	// OrderNo 嘉联分账单号
	OrderNo string `json:"order_no,omitempty"`
	// OutReturnNo 外部退款号
	OutReturnNo string `json:"out_return_no,omitempty"`
	// ReturnNo 嘉联退款号
	ReturnNo string `json:"return_no,omitempty"`
	// Amount 退款金额
	Amount interface{} `json:"amount,omitempty"`
	// Description 退款描述
	Description string `json:"description,omitempty"`
	// Result 结果
	Result string `json:"result,omitempty"`
	// FailReason 失败原因
	FailReason string `json:"fail_reason,omitempty"`
	// CreateTime 创建时间
	CreateTime string `json:"create_time,omitempty"`
	// FinishTime 完成时间
	FinishTime string `json:"finish_time,omitempty"`
}

// NewBalanceRefundQueryResponse 创建余额退款查询响应
func NewBalanceRefundQueryResponse() *BalanceRefundQueryResponse {
	return &BalanceRefundQueryResponse{}
}
